// 系统通知功能测试工具
// 用于测试通知管理相关的API接口

import { 
  getSystemNotificationsService,
  getSystemNotificationService,
  createSystemNotificationService,
  updateSystemNotificationService,
  deleteSystemNotificationService
} from '@/api/admin.js'

// 状态常量
const NOTIFICATION_STATUS = {
  DRAFT: 0,      // 草稿
  PUBLISHED: 1,  // 已发布
  DELETED: -1    // 已删除
}

// 测试数据
const testNotificationData = {
  title: '测试通知标题',
  content: '<p>这是一个<strong>测试通知</strong>的内容，包含<em>富文本</em>格式。</p><p>支持多段落内容。</p>',
  summary: '这是测试通知的摘要信息',
  status: NOTIFICATION_STATUS.PUBLISHED
}

// 测试创建通知
export const testCreateNotification = async () => {
  console.log('🧪 测试创建通知...')
  try {
    const result = await createSystemNotificationService(testNotificationData)
    console.log('✅ 创建通知成功:', result)
    return result.data
  } catch (error) {
    console.error('❌ 创建通知失败:', error)
    throw error
  }
}

// 测试获取通知列表
export const testGetNotificationsList = async (params = {}) => {
  console.log('🧪 测试获取通知列表...', params)
  try {
    const result = await getSystemNotificationsService(params)
    console.log('✅ 获取通知列表成功:', result)
    return result.data
  } catch (error) {
    console.error('❌ 获取通知列表失败:', error)
    throw error
  }
}

// 测试获取通知详情
export const testGetNotificationDetail = async (notificationId) => {
  console.log('🧪 测试获取通知详情...', notificationId)
  try {
    const result = await getSystemNotificationService(notificationId)
    console.log('✅ 获取通知详情成功:', result)
    return result.data
  } catch (error) {
    console.error('❌ 获取通知详情失败:', error)
    throw error
  }
}

// 测试更新通知
export const testUpdateNotification = async (notificationId, updateData) => {
  console.log('🧪 测试更新通知...', notificationId, updateData)
  try {
    const result = await updateSystemNotificationService(notificationId, updateData)
    console.log('✅ 更新通知成功:', result)
    return result.data
  } catch (error) {
    console.error('❌ 更新通知失败:', error)
    throw error
  }
}

// 测试删除通知
export const testDeleteNotification = async (notificationId) => {
  console.log('🧪 测试删除通知...', notificationId)
  try {
    const result = await deleteSystemNotificationService(notificationId)
    console.log('✅ 删除通知成功:', result)
    return result
  } catch (error) {
    console.error('❌ 删除通知失败:', error)
    throw error
  }
}

// 完整的功能测试流程
export const runFullNotificationTest = async () => {
  console.log('🚀 开始完整的通知功能测试...')
  
  try {
    // 1. 测试获取通知列表（初始状态）
    console.log('\n📋 步骤1: 获取初始通知列表')
    await testGetNotificationsList({ page: 1, size: 10 })
    
    // 2. 测试创建草稿通知
    console.log('\n📝 步骤2: 创建草稿通知')
    const draftData = { ...testNotificationData, status: NOTIFICATION_STATUS.DRAFT, title: '测试草稿通知' }
    const draftNotification = await testCreateNotification(draftData)
    
    // 3. 测试创建已发布通知
    console.log('\n📢 步骤3: 创建已发布通知')
    const publishedData = { ...testNotificationData, title: '测试已发布通知' }
    const publishedNotification = await testCreateNotification(publishedData)
    
    // 4. 测试获取通知详情
    console.log('\n🔍 步骤4: 获取通知详情')
    await testGetNotificationDetail(publishedNotification.id)
    
    // 5. 测试更新通知
    console.log('\n✏️ 步骤5: 更新通知')
    const updateData = {
      title: '更新后的通知标题',
      content: '<p>这是更新后的通知内容</p>',
      summary: '更新后的摘要',
      status: NOTIFICATION_STATUS.PUBLISHED
    }
    await testUpdateNotification(publishedNotification.id, updateData)
    
    // 6. 测试分页和筛选
    console.log('\n📄 步骤6: 测试分页和筛选')
    await testGetNotificationsList({ page: 1, size: 5, status: NOTIFICATION_STATUS.PUBLISHED })
    await testGetNotificationsList({ page: 1, size: 5, status: NOTIFICATION_STATUS.DRAFT })
    
    // 7. 测试删除通知
    console.log('\n🗑️ 步骤7: 删除测试通知')
    await testDeleteNotification(draftNotification.id)
    await testDeleteNotification(publishedNotification.id)
    
    // 8. 验证删除后的列表
    console.log('\n✅ 步骤8: 验证删除后的通知列表')
    await testGetNotificationsList({ page: 1, size: 10 })
    
    console.log('\n🎉 所有测试完成！')
    
  } catch (error) {
    console.error('\n💥 测试过程中出现错误:', error)
  }
}

// 分页测试
export const testPagination = async () => {
  console.log('🧪 测试分页功能...')
  
  try {
    // 测试不同页码和页面大小
    const testCases = [
      { page: 1, size: 5 },
      { page: 1, size: 10 },
      { page: 2, size: 5 },
      { page: 1, size: 20 }
    ]
    
    for (const params of testCases) {
      console.log(`📄 测试分页参数: page=${params.page}, size=${params.size}`)
      const result = await testGetNotificationsList(params)
      console.log(`   结果: 共${result.total}条记录，当前页${result.list?.length || 0}条`)
    }
    
    console.log('✅ 分页测试完成')
  } catch (error) {
    console.error('❌ 分页测试失败:', error)
  }
}

// 状态筛选测试
export const testStatusFilter = async () => {
  console.log('🧪 测试状态筛选功能...')
  
  try {
    // 测试不同状态筛选
    const statuses = [
      { status: '', name: '全部' },
      { status: NOTIFICATION_STATUS.PUBLISHED, name: '已发布' },
      { status: NOTIFICATION_STATUS.DRAFT, name: '草稿' }
    ]
    
    for (const { status, name } of statuses) {
      console.log(`🔍 测试筛选: ${name}`)
      const params = { page: 1, size: 10 }
      if (status !== '') {
        params.status = status
      }
      const result = await testGetNotificationsList(params)
      console.log(`   结果: ${name}状态下共${result.total}条记录`)
    }
    
    console.log('✅ 状态筛选测试完成')
  } catch (error) {
    console.error('❌ 状态筛选测试失败:', error)
  }
}

// 导出测试函数到全局，方便在浏览器控制台中调用
if (typeof window !== 'undefined') {
  window.notificationTest = {
    runFullTest: runFullNotificationTest,
    testPagination,
    testStatusFilter,
    testCreate: testCreateNotification,
    testList: testGetNotificationsList,
    testDetail: testGetNotificationDetail,
    testUpdate: testUpdateNotification,
    testDelete: testDeleteNotification
  }
  
  console.log('🔧 通知测试工具已加载到 window.notificationTest')
  console.log('💡 使用方法:')
  console.log('   window.notificationTest.runFullTest() - 运行完整测试')
  console.log('   window.notificationTest.testPagination() - 测试分页')
  console.log('   window.notificationTest.testStatusFilter() - 测试状态筛选')
}
